package com.service.Authorization.controller;

import com.service.Authorization.dto.AuthorizationDto;
import com.service.Authorization.dto.FindAccountAndPasswordDto;
import com.service.Authorization.service.AuthorizationUserService;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.RegexUtil;
import com.service.config.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author
 * @version 1.0
 * @date 2020/8/20 15:57
 */
@Api(value = "第三方认证", tags = "第三方认证")
@Controller
public class AuthorizationController {
    private static final Logger LOG = LoggerFactory.getLogger(AuthorizationController.class);
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AuthorizationUserService authorizationUserService;
    /**
     *授权终端用户
     * @param authorizationVo
     * @return
     */
    @ApiOperation(value = "根据mac情况授权")
    @PostMapping("/authorizationUserNoPassword")
    @ResponseBody
    public Result authorizationUserNoPassword(@RequestBody AuthorizationDto authorizationVo) throws Exception {
        return authorizationUserService.authorizationUserNoPassword(authorizationVo);
    }
    /**
     *授权终端用户
     * @param authorizationVo
     * @return
     */
    @ApiOperation(value = "授权终端用户")
    @PostMapping("/authorizationU")
    @ResponseBody
    public Result authorizationUser(@RequestBody AuthorizationDto authorizationVo) throws Exception {
        return authorizationUserService.setAuthorization(authorizationVo);
    }
    /**
     * 获取账号和密码
     * @param findAccountAndPasswordDto
     * @return
     */
    @ApiOperation(value = "获取账号和密码")
    @PostMapping("/getUserNameAndPassword")
    @ResponseBody
    public Result getUserNameAndPassword(@RequestBody FindAccountAndPasswordDto findAccountAndPasswordDto) throws Exception {
        if (findAccountAndPasswordDto.getType() == 1) {
            if (StringUtils.isEmpty(findAccountAndPasswordDto.getNumber())) {
                return Result.failure( 401, "参数不对");
            }
            if (!RegexUtil.isMobileSimple(findAccountAndPasswordDto.getNumber())) {
                return Result.failure( 402, "参数值不对");
            }
        }
        return Result.ok().setData(authorizationUserService.getUserNameAndPassword(findAccountAndPasswordDto));
    }
}
